package com.lp.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.lp.DefineException.DBHelperException;
import com.lp.entity.User;
import com.lp.utils.DBHelper;
import com.lp.utils.RowToObject;

public class UserDAO
{
	private DBHelper<User> dbHelper;
	
	public UserDAO()
	{
		dbHelper = new DBHelper<User>();
	}
	
	public User findUserByUserAccountAndPassword(String user_account,String user_password)
	{
		String sql = "select * from project_user where user_account = ? and user_password = ?";
		
		return dbHelper.myExecuteQuerySingle(sql, new RowToUser(), user_account,user_password);
	}
	
	private class RowToUser implements RowToObject<User>
	{

		public User rowToObject(ResultSet rs)
		{
			User user = new User();
			try
			{
				user.setId(rs.getInt("id"));
				user.setUser_account(rs.getString("user_account"));
				user.setUser_password(rs.getString("user_password"));
				user.setUser_name(rs.getString("user_name"));
				user.setUser_lastAccessTime(rs.getString("user_lastAccessTime"));
				user.setUser_lastAccessIp(rs.getString("user_lastAccessIp"));
			}
			catch (SQLException e)
			{
				throw new DBHelperException(e.getMessage(),e);
			}
			
			return user;
		}
		
	}

}
